// MainController
function mainCtrl($scope, $route, $rootScope, $timeout, commonService, $location, $sce) {

    _log('### MainController ##');

    $rootScope.pageData = _pageData;
    $rootScope.viewData = _viewData;
    $rootScope.adminPage = false;
    $rootScope.hasLeftMenu = true;
    $rootScope.adminLoginPage = false;
    $rootScope.showImageSlider = false;
    $rootScope.enum = _COMMON.enum;

    //Defined _pageData
    _pageData.isAuthen = false;  
    _pageData.tokenKey = _COMMON.token;
    _pageData.user = (_pageData.user != null) ? _pageData.user : null;

    var getViewClassFromRoute = function () {
        var viewClass = "";
        viewClass = $route.current.templateUrl;
        viewClass = viewClass.replace(/^.*\//, "");
        viewClass = viewClass.replace(".html", "");

        return viewClass;
    };

    $rootScope.renderHtml = function (html_code) {
        return $sce.trustAsHtml(html_code);
    };

    $rootScope.renderTxtLimit = function (txt, limit) {
        return txt.length > limit ? txt.substr(0, limit) + '...' : txt;
    };


    $scope.$on("$locationChangeSuccess", function () {

        var viewClass = getViewClassFromRoute();
        $scope.activePage = viewClass; // For active menu css

        $scope.includePage = '';

        _log('#### URL Localhost : ' + location.href);
        _log('#### $scope.activePage : ' + viewClass);
        var url = location.href.split('#')[1];
        if (url != undefined && url.match('admin') != null) {

            $rootScope.adminPage = true; //admin url

            _COMMON.loading(500);

            //check authentication or not?
            if (_COMMON.getPageData()) {
                _log('### Authenitication ###');
            } else {
                _log('### Not authenitication ###');
                $location.path('admin/login');
            }

        } else {
            $rootScope.adminPage = false; //normally url 
            $rootScope.hasLeftMenu = true;
            $rootScope.contentWidth = "735px";
            _COMMON.loading(4000);
        }

        $rootScope.showImageSlider = false;

    });


    /*------- Left Menu -------*/
    $scope.downloadItems = [];

    var url = 'content_ws.php';
    var params = {
        wsname: 'getcontentbycategoryid',
        catid: 6, // Common
        token: _pageData.tokenKey
    };
    $.getJSON(_COMMON.context + url, params).done(function (result) {
        $scope.$apply(function () {
            var datas = result.contents;
            for (i in datas) {
                if (datas[i]['alias'] == 'download') {
                    $scope.downloadItems.push(datas[i].introtext);
                }
                if (datas[i]['alias'] == 'footer_alias') { // TODO
                    _pageData['footer'] = datas[i].fulltext;
                }
                if (datas[i]['alias'] == 'left_contact') { // TODO
                    _pageData['left_contact'] = datas[i].fulltext;
                }
            }

        });
    });


    /*------- config header -------*/
    var params = {
        wsname: 'getcontentbyid',
        id: 11, 
        token: _pageData.tokenKey
    };
    $.getJSON(_COMMON.context + url, params).done(function (result) {
        $scope.$apply(function () {
            
            _pageData.headerImageObj = result.contents;
            
            $timeout(function () {
                var el = $('#hidden-img-hdr img').attr('src');
                $('#header').css('background-image', 'url(' + el + ')');
            }, 1000);
            
        });
    });


   

    _pageData.linkImage = [ {
	src : 'app/images/link/yota-banner.jpg',
	url : 'http://www.yotafoam.com/'
    }, {
	src : 'app/images/link/coolblock.jpg',
	url : 'http://www.coolblock.co.th'
    }, {
	src : 'app/images/link/buafoam-banner.jpg',
	url : 'http://www.buafoam.com'
    }, {
	src : 'app/images/link/foamcrete-banner.jpg',
	url : 'http://www.geofoamthailand.com'
    }, {
	src : 'app/images/link/raft.jpg',
	url : 'http://www.cementfoam.com'
    }, {
	src : 'app/images/link/TPFRIA.jpg',
	url : 'http://www.baanfoam.com/'
    } ];
}